﻿
Ext.define('app.ux.UploadPanel', {
    extend: 'Ext.panel.Panel',
    border: false,
    hiddenEdit: false,
    labelName: '附件:',
    buttonName: '上传',
    layout: 'column',
    fileExtent: ['.doc', '.xls', '.xlsx', '.docx', '.pdf'], //限制上传的文件类型
    enableFileProtect: true, //是否启用文件类型限制
    initComponent: function () {
        var me = this;
        me.docs = new Array();
        me.items = [me.getUploadButton()];
        me.callParent();
    },
    getUploadButton: function () {
        var me = this;
        if (!this.uploadform) {
            this.uploadform = new Ext.form.Panel(
            {
                border: false,
                layout: 'column',
                items: [
                    {
                        border: false,
                        bodyStyle:"padding-left:20px;",
                        html: me.labelName
                    },
                {
                    xtype: 'filefield',
                    width: 60,
                    hidden: me.hiddenEdit,
                    name: 'docfile',
                    buttonOnly: true,
                    buttonText: me.buttonName,
                    listeners:
                    {
                        'change': function (file, value, eOpts) {
                            if (me.enableFileProtect) {
                                console.log('判断文件类型');
                                var fileExtent = value.substr(value.lastIndexOf(".")).toLowerCase();
                                var uploadAllowed = false;
                                for (var i = 0; i < me.fileExtent.length; i++) {
                                    if (me.fileExtent[i] == fileExtent) {
                                        uploadAllowed = true;
                                    }
                                }

                                if (uploadAllowed == false) {
                                    //if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) { 
                                    alert("只允许上传" + me.fileExtent.join('|') + "格式的文件");

                                    return;
                                }
                            }

                            me.uploadform.getForm().submit(
                            {
                                url: "/prm/document/UploadFile",
                                success: function (form, action) {
                                    var json = Ext.decode(action.response.responseText);
                                    me.addFile(json.result.id, json.result.name, json.result.size, json.result.path, json.result.extension);
                                }
                            });
                        }
                    }
                }]
            }
            );
        }
        return this.uploadform;
    },
    addFile: function (id, name, size, path, extension) {
        var main = this;
        var panel = new Ext.panel.Panel(
        {
            border: false,
            layout: 'table',
            style: 'margin:0px 5px 0px 0px',
            items: [
                { xtype: 'displayfield', style: 'margin:0px 5px 0px 5px', value: name + '  <a style="color:red;">' + size + " Kb" + "</a>" },
                { xtype: 'displayfield',  style: 'margin:0px 5px 0px 5px', value: '<a href="/api/services/app/document/GetFile?path=' + path + '" style="color:blue;text-decoration:underline;cursor:pointer">下载</a>' },
                {
                    xtype: 'displayfield', hidden: main.hiddenEdit, style: 'margin:0px 5px 0px 5px', value: '<a style="color:blue;text-decoration:underline;cursor:pointer">删除</a>',
                    listeners:
                    {
                        afterrender: function (component) {
                            component.getEl().on('click', function () {
                                main.removeArrys(id);
                                main.remove(panel);
                                //main.doLayout();
                            });
                        }
                    }
                }
            ]
        });
        main.add(panel);
        //将上传成功的文件Id加入到数组中
        this.docs.push({ Id: id, Name: name, Size: size, FullPath: path, Extension: extension });
    },
    removeArrys: function (docId) {
        for (var i = 0; i < this.docs.length; i++) {
            var obj = this.docs[i];
            if (obj.Id == docId) {
                Ext.Array.removeAt(this.docs, i);
            }
        }
    },
    getValue: function () {
        return this.docs;
    },
    setValue: function (attachs) {
        for (var i = 0; i < attachs.length; i++) {
            var att = attachs[i];
            this.addFile(att.Id, att.Name, att.Size, att.Path || att.FullPath, att.Extension, att.LinkUrl);
        }
    },
    removeAllArry: function () {
        var count = this.items.items.length;

        var removeArry = [];
        for (var i = 1; i < count; i++) {
            removeArry.push(this.items.items[i]);
        }

        for (var i = 0; i < removeArry.length; i++) {
            this.remove(removeArry[i]);
        }

        this.docs = [];
    }
});